/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import Entitys.Cargo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author MAURO
 */
public class CargoServices {
    EntityManagerFactory emf= Persistence.createEntityManagerFactory("DrugStoreProPU");
         EntityManager em=emf.createEntityManager();
         
         public void create (Cargo c){
          em.getTransaction().begin();
          em.persist(c);
          em.getTransaction().commit();
          em.close();
         }
         
         
          public void delete (Integer  p){
          em.getTransaction().begin();
          Cargo p1=em.find(Cargo.class, p);
          if(p1!= null)
          em.remove(p1);
          em.getTransaction().commit();
          em.close();
         }
          
          public Cargo readById(Integer idc){
           Query q= em.createNamedQuery("Cargo.findByIdCargo");
           q.setParameter("idCargo", idc);
           Cargo cg=(Cargo) q.getSingleResult();
           return cg;
          }
          
          public List<Cargo> readAll(){
            Query q= em.createNamedQuery("Cargo.findAll");    
            return q.getResultList();
          }
          
          public Cargo ReadByType(String type){
            Query q= em.createNamedQuery("Cargo.findByTipoCargo"); 
            q.setParameter("tipoCargo", type);
            Cargo cg=(Cargo) q.getSingleResult();
          return cg;
          }
    
}
